package net.sf.jimex.project;

import net.sf.jimex.project.util.ProjectCalendar;

import java.util.*;

/**
 * andrew 23.01.2006 22:39:11
 */
public class Project {
    private List tasks;
    private HashMap resourcesMap;
    private List assignments;
    private Map taskMap;
    private Stack taskStack;
    private int taskLevel;

    private ProjectCalendar calendar;

    public Project() {
        calendar = createCalendar();
        tasks = new ArrayList();
        resourcesMap = new HashMap();
        assignments = new ArrayList();
        taskMap = new HashMap();
        taskStack = new Stack();
        taskLevel = 0;
    }

    private static ProjectCalendar createCalendar() {
        return new ProjectCalendar();
    }

    public List getAllTasks() {
        return tasks;
    }

    public void addTask(Task task) {
        String taskId = task.getId();
        String outlineLevelProperty = task.getProperty("OutlineLevel");
        if (outlineLevelProperty == null || "".equals(outlineLevelProperty))
            return;
        int outlineLevel = Integer.valueOf(outlineLevelProperty).intValue();

        // don't import first empty task
        if (outlineLevel == 1) {
            tasks.add(task);
        }
        if (taskLevel == outlineLevel) {
            if (taskStack.size() != 0) {
                taskStack.pop();
            }
            if (taskStack.size() > 0) {
                Task parentTask = (Task) taskStack.lastElement();
                if (parentTask != null) {
                    parentTask.addChild(task);
                }
            }
            taskStack.push(task);
        } else if (outlineLevel > taskLevel) {
            Task parentTask = (Task) taskStack.lastElement();
            if (parentTask != null) {
                parentTask.addChild(task);
            }
            taskStack.push(task);
        } else if (outlineLevel < taskLevel) {
            taskStack.pop();
            if (taskStack.size() != 0) {
                taskStack.pop();
            }
            if (taskStack.size() > 0) {
                Task parentTask = (Task) taskStack.lastElement();
                if (parentTask != null) {
                    parentTask.addChild(task);
                }
            }
            taskStack.push(task);
        }
        taskMap.put(taskId, task);
        taskLevel = outlineLevel;
    }

    public void addResource(Resource resource) {
        String propertyId = resource.getId();
        resourcesMap.put(propertyId, resource);
    }

    public void addAssignment(Assignment assignment) {
        assignments.add(assignment);
    }

    public void prepare() {
        for (Iterator iterator = assignments.iterator(); iterator.hasNext();) {
            Assignment assignment = (Assignment) iterator.next();
            String taskId = assignment.getTaskId();
            String resourceId = assignment.getResourceId();
            Task task = (Task) taskMap.get(taskId);
            Resource resource = (Resource) resourcesMap.get(resourceId);
            if (task != null && resource != null)
                task.setAssignee(resource.getUsername());
        }
    }

    public void setMinutesPerDay(String minutesPerDay) {
        calendar.setMinutesPerDay(Integer.parseInt(minutesPerDay));
    }

    public void setMinutesPerWeek(String minutesPerWeek) {
        calendar.setMinutesPerWeek(Integer.parseInt(minutesPerWeek));
    }

    public void setDaysPerMonth(String daysPerMonth) {
        calendar.setDaysPerMonth(Integer.parseInt(daysPerMonth));
    }

    public ProjectCalendar getCalendar() {
        return calendar;
    }
}
